using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParallaxBackground : MonoBehaviour
{
    private Camera mainCamera;
    private float lastCameraPositionX;
    private float cameraHalfWidth;

    [SerializeField] private ParallaxLayer[] backgroundLayers;

    private void Awake()
    {
        mainCamera = Camera.main;
        cameraHalfWidth =mainCamera.orthographicSize * mainCamera.aspect;
        InitializeLayers();
    }

    private void FixedUpdate()
    {
        float currentcameraPositionX = mainCamera.transform.position.x;
        float distanceToMove = currentcameraPositionX - lastCameraPositionX;
        lastCameraPositionX = currentcameraPositionX;

        float cameraLeftEdge = currentcameraPositionX - cameraHalfWidth;
        float cameraRightEdge = currentcameraPositionX + cameraHalfWidth;

        foreach (ParallaxLayer layer in backgroundLayers) 
        { 
            layer.Move(distanceToMove);    
            layer.LoopBackground(cameraLeftEdge, cameraRightEdge);
        }
    }
        
    private void InitializeLayers()
    {
        foreach (ParallaxLayer layer in backgroundLayers)
            layer.CalculateImageWidth();
    }


}
